package com.cxcs.cxcsojbackendmodel.vo;

import cn.hutool.json.JSONUtil;
import com.cxcs.cxcsojbackendmodel.dto.question.JudgeConfig;
import com.cxcs.cxcsojbackendmodel.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;

/**
 * 题目封装类——返回前端
 *
 * @Author: CxCS
 * @Version: 1.0
 */
@Data
public class QuestionVO {
    /**
     * id
     */
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tags;

    /**
     * 题目提交数
     */
    private Integer submitNum;

    /**
     * 题目通过数
     */
    private Integer acceptedNum;

    /**
     * 判题配置（json 对象）
     */
    private JudgeConfig judgeConfig;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     *  创建题目人的信息
     */
    private UserVO userVO;

    /**
     * 包装类转对象
     *      对象转JSON字符串
     * @param questionVO
     * @return
     */
    public static Question voToObj(QuestionVO questionVO) {
        if (questionVO == null) {
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionVO, question);

        List<String> tagList = questionVO.getTags();  // 标签列表
        if (tagList != null) {
            question.setTags(JSONUtil.toJsonStr(tagList));
        }

        JudgeConfig judgeConfig1 = questionVO.getJudgeConfig();
        if(judgeConfig1 != null) {
            question.setJudgeConfig(JSONUtil.toJsonStr(judgeConfig1));
        }

        return question;
    }

    /**
     * 对象转包装类
     *      将JSon字符串转为 Java对象
     * @param question
     * @return
     */
    public static QuestionVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionVO questionVO = new QuestionVO();
        BeanUtils.copyProperties(question, questionVO);

        List<String> tagList = JSONUtil.toList(question.getTags(), String.class);  // 字符串转集合
        questionVO.setTags(tagList);

        String judgeConfigStr = question.getJudgeConfig();
        questionVO.setJudgeConfig(JSONUtil.toBean(judgeConfigStr, JudgeConfig.class));  // 字符串转JudgeConfig并存入
        return questionVO;
    }
    
    private static final long serialVersionUID = 1L;
    
    
}
